package com.itheima.servlet;

import com.itheima.domain.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/Role/*")
public class RoleServlet extends BaseServlet {

    protected void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //先手封装
        User user1 = new User("a1","b");
        User user2 = new User("a2","b");
        User user3 = new User("a3","b");
        //创建集合
        List<User> list = new ArrayList<>();
        list.add(user1);
        list.add(user2);
        list.add(user3);
        //存到作用域
        req.setAttribute("list",list);
        //跳转
        req.getRequestDispatcher("/page/demo3.jsp").forward(req,resp);
    }

    protected void saveRole(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("roleservlet");
        //二者只可选其一使用
        //重定向
        //resp.sendRedirect(req.getContextPath()+"/user/findAll");
        //转发请求
        String username = "admin";
        //将username存入作用域中
        req.setAttribute("name",username);
        /*req.getRequestDispatcher("/page/demo3.jsp").forward(req,resp);*/
        req.getRequestDispatcher("/page/demo3.jsp").forward(req,resp);
    }
}
